﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Smart.Windows.Navigation;

namespace NavigationSampleApp
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MainForm : Form, IFormControl
    {
        private readonly Navigator navigator;

        /// <summary>
        /// 
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;

            // コントローラー初期化
            navigator = new Navigator( new ControlViewProvider( ViewPanel ) );
            navigator.Forwarding += OnForwarding;
            navigator.Exited += OnExited;

            navigator.AutoRegister( Assembly.GetExecutingAssembly() );

            // 最初の画面へ遷移
            Show();
            navigator.Forward( Views.Menu );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnForwarding(object sender, ViewForwardEventArgs e)
        {
            IApplicationView view = navigator.CurrentView as IApplicationView;
            if( view != null )
            {
                view.SetFormControl( this );
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnExited(object sender, ViewExitEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        public void SetTitle(string text)
        {
            this.lblTitle.Text = text;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        public void SetButtonLeft(string text)
        {
            this.btnLeft.Text = text ?? "";
            this.btnLeft.Enabled = text != null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        public void SetButtonRight(string text)
        {
            this.btnRight.Text = text ?? "";
            this.btnRight.Enabled = text != null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLeft_Click(object sender, EventArgs e)
        {
            IApplicationView view = navigator.CurrentView as IApplicationView;
            if( view != null )
            {
                view.OnButtonLeft();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRight_Click(object sender, EventArgs e)
        {
            IApplicationView view = navigator.CurrentView as IApplicationView;
            if( view != null )
            {
                view.OnButtonRight();
            }
        }
    }
}